package com.egnaro.uberAdminFramework.ui {
	
	import flash.desktop.NativeApplication;
	import flash.desktop.DockIcon;
	import flash.desktop.SystemTrayIcon;
	import flash.display.Bitmap;
	import flash.utils.describeType;
    import flash.utils.getDefinitionByName;
	
	public class SysTrayNotify {
		
		// constants
		private const _ONLINE:String = "online";
		private const _OFFLINE:String = "offline";
		
		// variables
		private var status:String = _ONLINE;
		public var className:String;
				
		// assets
		[Embed (source="assets/icons/now_logo_128.png")]
		[Bindable]private var appOK:Class;
		[Embed (source="assets/icons/now_logo_alert_128.png")]
		[Bindable]private var appAlert:Class;
		
		// constructor
		public function SysTrayNotify(newStatus:String) {
			setStatus(newStatus);
		}
		
				
		public function setStatus(newStatus:String):void {
			if(newStatus == "online"){
				status = _ONLINE;
			}
			
			if(newStatus == "offline"){
				status = _OFFLINE;
			}
			
			setSystemTrayOrDock();
		}
		
		private function setSystemTrayOrDock():void {
			var sysTrayIcon:SystemTrayIcon; // Windows
			var dockIcon:DockIcon; // Mac
			var statusIcon:Bitmap;
			if(status == _ONLINE){
				statusIcon = createImage("appOK");	
			}
			if(status == _OFFLINE){
				statusIcon = createImage("appAlert");	
			}
			
			// check for support
			if(NativeApplication.supportsSystemTrayIcon){
				sysTrayIcon = NativeApplication.nativeApplication.icon as SystemTrayIcon;
				sysTrayIcon.tooltip = "UberAdmin";
				sysTrayIcon.bitmaps = [statusIcon];
			}
			
			if(NativeApplication.supportsDockIcon){
				dockIcon = NativeApplication.nativeApplication.icon as DockIcon;
				dockIcon.bitmaps = [statusIcon];
			}
		}
		
		private function createImage(embedLinkage:String):Bitmap {
            if(className == null) {
                className = describeType(this).@name.toXMLString();
            }
            var imgName:String = className + "_" + embedLinkage;
            var imgRef:Class = getDefinitionByName(imgName) as Class;
            var img:Bitmap = new imgRef();
            return img;
        }

	}
}